import { withUser, ApiError } from "@/lib/api"; import { exportConversation, EXPORT_FORMATS, type ExportFormat } from "@/lib/conversations/service"; import { APP_URL } from "@/lib/env"; export const dynamic = "force-dynamic"; type P = { id: string }; /** * GET /api/conversations/[id]/export?format=json|markdown|txt|html[&download=1][&print=1] * * - `download=1` → `Content-Disposition: attachment` (default for json/markdown/txt) * - html without `download` renders inline (opened in a new tab by the client for "PDF (print)"); * `print=1` embeds a tiny script that opens the print dialog on load. */ export const GET = withUser
( async ({ req, user }, { id }) => { const sp = new URL(req.url).searchParams; const format = (sp.get("format") ?? "markdown") as ExportFormat; if (!(EXPORT_FORMATS as readonly string[]).includes(format)) throw new ApiError(400, `Unknown format. Use one of: ${EXPORT_FORMATS.join(", ")}`, "BAD_FORMAT"); const print = sp.get("print") === "1"; const inline = format === "html" && sp.get("download") !== "1"; const out = await exportConversation(user.id, id, format, { print, appUrl: APP_URL }); return new Response(out.body, { headers: { "Content-Type": out.contentType, "Cache-Control": "private, no-store", "X-Robots-Tag": "noindex", ...(inline ? {} : { "Content-Disposition": `attachment; filename="${out.filename}"` }), }, }); }, { limit: { max: 40, windowMs: 60_000, key: "conv-export" } }, );